package net.sppan.base.task;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import net.sppan.base.config.redis.RedisTemplateUtil;
import net.sppan.base.entity.CommentExuser;
import net.sppan.base.entity.DyContentComment;
import net.sppan.base.entity.DyMedia;
import net.sppan.base.entity.DyuserBlocked;
import net.sppan.base.service.ICommentExuserService;
import net.sppan.base.service.IDyContentCommentService;
import net.sppan.base.service.IDyMediaService;
import net.sppan.base.service.IDyuserBlockedService;
import net.sppan.base.service.specification.SimpleSpecificationBuilder;
import net.sppan.base.service.specification.SpecificationOperator.Operator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;

@Component
@Configuration
public class ContentCommentTask {
  
	@Autowired
	private IDyContentCommentService contentCommentService;
	@Autowired
	private ICommentExuserService commentExuserService;
	@Autowired
	private IDyMediaService dyMediaService;
	@Autowired
	private RedisTemplateUtil redisTemplateUtil;
	@Autowired
	private IDyuserBlockedService dyuserBlockedService;
	
	
	@Scheduled(fixedRate = 300000)
	public void synContentComment(){
//		redisTemplateUtil.del("examine:comment:list:last");
		String idv = (String) redisTemplateUtil.get("examine:comment:list:last");
		if(idv==null||idv.equals("")){
			Page<CommentExuser> pageExuser= commentExuserService.findAll(new PageRequest(0, 1, new Sort(Direction.DESC, "commentId")));
			if(!pageExuser.getContent().isEmpty()){
				idv=String.valueOf(pageExuser.getContent().get(0).getCommentId());
			}else{
				idv = "0";
			}
		}
		Page<DyContentComment> page= contentCommentService.findContentComment(Long.valueOf(idv),new PageRequest(0, 1000, new Sort(Direction.DESC, "commentId")));
		if(page.getTotalElements()>0){
			List<CommentExuser> commentExusers =new ArrayList<CommentExuser>();
			String idValue=""; 
			for (DyContentComment contentComment: page.getContent()) {
				CommentExuser commentExuser =new CommentExuser();
				commentExuser.setCommentContent(contentComment.getCommentContent());
				commentExuser.setCommentDyId(contentComment.getCommentDyId());
				commentExuser.setCommentId(contentComment.getCommentId());
				commentExuser.setCommentType(contentComment.getCommentType());
				commentExuser.setDyContentId(contentComment.getDyContentId());
				commentExuser.setMediaIds(contentComment.getMediaIds()); 
				commentExuser.setReplyToCommentId(contentComment.getReplyToCommentId());
				commentExuser.setReplyToDyId(contentComment.getReplyToDyId());
				commentExuser.setType(contentComment.getType());
				commentExuser.setUploadDate(contentComment.getUploadDate());
				if(contentComment.getMediaIds()!=null&&!contentComment.getMediaIds().trim().equals("")){
					SimpleSpecificationBuilder<DyMedia> builderMedia = new SimpleSpecificationBuilder<DyMedia>();
					builderMedia.add("mediaId", Operator.in.name(), contentComment.getMediaIds());
					Page<DyMedia> mediaList =dyMediaService.findAll(builderMedia.generateSpecification(),new PageRequest(0, 1000, new Sort(Direction.DESC, "mediaId")));
					commentExuser.setMediaJson(JSON.toJSONString(mediaList.getContent()));
				}
				
				SimpleSpecificationBuilder<DyuserBlocked> builder = new SimpleSpecificationBuilder<DyuserBlocked>();
				builder.add("dyId",Operator.eq.name(), contentComment.getCommentDyId());
				Page<DyuserBlocked> page2=dyuserBlockedService.findAll(builder.generateSpecification(),new PageRequest(0, 1, new Sort(Direction.DESC, "createTime")));
				if(page2.getTotalElements()>0){
					List<DyuserBlocked>  dyuserBlockeds = page2.getContent();
					Date date =new Date();
					if(!dyuserBlockeds.get(0).getBlockedType().equals("0")){//非正常 用戶  
						if(date.before(dyuserBlockeds.get(0).getBlockedEndtime())||dyuserBlockeds.get(0).getBlockedType().equals("4")){
							commentExuser.setExResult("3");
							commentExuser.setExDesc("系统导入自动处理为 不通过");
							//禁言下 自动设置 评论为不通过
							contentComment.setCommentType(3L);
							contentCommentService.update(contentComment);
						}else{
							commentExuser.setExResult("0");
						}
					}else{
						commentExuser.setExResult("0");
					} 
				}else{
					commentExuser.setExResult("0");
				}
				commentExuser.setInputTime(new Date());
				commentExusers.add(commentExuser);
				if(idValue.equals("")){
				   idValue=String.valueOf(contentComment.getCommentId());
				}
			}
			redisTemplateUtil.set("examine:comment:list:last", idValue,600000);
			commentExuserService.saveAll(commentExusers);
		}
	}
	
	
	@Scheduled(fixedRate = 2040000)
	public void clearCommentTask(){
		SimpleSpecificationBuilder<CommentExuser> builder = new SimpleSpecificationBuilder<CommentExuser>();
		Calendar nowAfter = Calendar.getInstance();
		nowAfter.add(Calendar.MINUTE, -30);
		builder.add("allotTime", Operator.lessThanOrEqualTo.name(), nowAfter.getTime());
		builder.add("exResult", Operator.eq.name(), "0");
		List<CommentExuser>  commentExusers =commentExuserService.findList(builder.generateSpecification(),new Sort(Direction.DESC,"id"));
		if(!commentExusers.isEmpty()&&commentExusers.size()>0){
			for(CommentExuser contentComment:commentExusers){
				CommentExuser commentExuserEx=commentExuserService.find(contentComment.getId());
				if(commentExuserEx.getExResult().equals("0")){
					contentComment.setExResult("0");
					contentComment.setAllotTime(null);
					contentComment.setExuserId(null);
					commentExuserService.update(contentComment);
				}
			}
		}
	}
}
	 
